package cn.bdqn.service.impl;

import cn.bdqn.dao.EmployeesMapper;
import cn.bdqn.pojo.Employees;
import cn.bdqn.pojo.Integral;
import cn.bdqn.service.EmployeesService;
import cn.bdqn.util.WeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class EmployeesServiceImpl implements EmployeesService {
    //固定注册微信的信息
    private String appid = "wx9168f76f000a0d4c";
    private String secret = "8ba69d5639242c3bd3a69dffe84336c1";
    private String accessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
    private String wxInfoUrl = "https://api.weixin.qq.com/sns/userinfo";
    @Autowired
    EmployeesMapper employeesMapper;

    @Override
    public List<Employees> listAllByid321(Integer id) {
        return employeesMapper.listAllByid321(id);
    }

    @Override
    public Employees findByOpenid(String code) {
        Employees employees=null;
        //1.根据code获取access_token和openId
        String atUtl = accessTokenUrl + "?code="+code+"&appid="+appid+"&secret="+secret+"&grant_type=authorization_code";
        System.out.println(atUtl);
        Map<String, Object> map1 = null;
        try {
            map1 = WeUtil.sendGet(atUtl);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Object access_token = map1.get("access_token");
        Object openid = map1.get("openid").toString();
        if(access_token == null && openid == null) {
            return employees;
        }
        System.out.println(openid.toString());
        //2.根据openId判断用户是否存在
        employees = employeesMapper.findByOpenid(openid.toString());
        return employees;
    }

    @Override
    public List<Employees> listAll() {
        return employeesMapper.listAll();
    }

    @Override
    public int del(Integer id) {
        return employeesMapper.del(id);
    }

    @Override
    public int insert(Employees employees) {
        return employeesMapper.insert(employees);
    }

    @Override
    public List<Employees> listAllByid(Integer id) {
        return employeesMapper.listAllByid(id);
    }

    @Override
    public Integer upd(Employees employees) {
        return employeesMapper.upd(employees);
    }

    @Override
    public Employees getById(int id) {
        return employeesMapper.getById(id);
    }

    //查询积分详情
    @Override
    public List<Integral> listAllByid123(Integer id) {
        return employeesMapper.listAllByid123(id);
    }

    @Override
    public List<Integral> getEmps(Integer id, Integer pn) {
        return employeesMapper.getEmps(id,pn);
    }

    @Override
    public Integer getCount(Integer id) {
        return employeesMapper.getCount(id);
    }

    @Override
    public Integer login(Employees employees) {
        return employeesMapper.login(employees);
    }

    @Override
    public String password(Employees employees) {
        return employeesMapper.password(employees);
    }

    @Override
    public int updateScoer(Employees employees) {
        return employeesMapper.updateScoer(employees);
    }

    @Override
    public Employees selectByPrimaryKey(Integer id) {
        return employeesMapper.selectByPrimaryKey(id);
    }

    @Override
    public Employees selectByUserName(String name) {
        return employeesMapper.selectByUserName(name);
    }

    @Override
    public Employees selectByUserNameOrEmail(String userNameOrEmail) {
        return employeesMapper.selectByUserNameOrEmail(userNameOrEmail);
    }

    @Override
    public int updateByPrimaryKey(Employees record) {
        return employeesMapper.updateByPrimaryKey(record);
    }
}
